package com.zm.moves;

import com.zm.moves.entity.Move;
import com.zm.moves.mapper.MoveMapper;
import com.zm.moves.mapper.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test {
    @Test
    public void  test() throws IOException {

        SqlSession sqlSession = MyBatisHelper.getSession();
        MoveMapper mapper = sqlSession.getMapper(MoveMapper.class);

        Document document = Jsoup.parse(new URL("https://www.dygangs.net/"),5000);
        Elements elements = document.select(".bg-fleet a");
        elements.remove(0);
        elements.remove(elements.size()-1);
        elements.remove(elements.size()-1);
        List<Map<String,Object>> types = new ArrayList<>();
        for (Element element : elements) {
            List<Map<String,Object>> moves = new ArrayList<>();
            String type = element.text();
            String href = element.attr("href");
            System.out.println("href = " + href);
            System.out.println("type = " + type);
            int count = 0;
            for (int i = 1;i<=5;i++){
                if(i==1){
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href),5000);
                }else{
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href +"index_" + i + ".htm" ),5000);
                }
                Elements elements1 = document.select(".border1 a");
                for (Element element1 : elements1) {
                    String href1 = element1.attr("href");
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href1),5000);
                    Element element2 = document.selectFirst("#dede_content>p");
                    String image = element2.select("img").attr("src");

                    String desc = element2.text();
                    if("".equals(desc)||" ".equals(desc)){
                        desc =  document.select("#dede_content>p").get(1).text();
                        if("".equals(desc)||" ".equals(desc)){
                            desc =  document.select("#dede_content>p").get(2).text();
                        }
                    }
                    Map<String,String> map = new HashMap<>();
                    String[] strings = desc.split("◎");
                    for (String string : strings) {
                        if(string.indexOf("片　　名")>-1){
                            map.put("片　　名",string.substring("片　　名".length()+1));
                        } else if(string.indexOf("产　　地")>-1){
                            map.put("产　　地",string.substring("产　　地".length()+1));
                        }else if(string.indexOf("类　　别")>-1){
                            map.put("类　　别",string.substring("类　　别".length()+1));
                        }else if(string.indexOf("语　　言")>-1){
                            map.put("语　　言",string.substring("语　　言".length()+1));
                        }else if(string.indexOf("上映日期")>-1){
                            map.put("上映日期",string.substring("上映日期".length()+1));
                        }else if(string.indexOf("IMDb评分")>-1){
                            map.put("IMDb评分",string.substring("IMDb评分".length()+1));
                        }else if(string.indexOf("豆瓣评分")>-1){
                            map.put("豆瓣评分",string.substring("豆瓣评分".length()+1));
                        }else if(string.indexOf("片　　长")>-1){
                            map.put("片　　长",string.substring("片　　长".length()+1));
                        }else if(string.indexOf("导　　演")>-1){
                            map.put("导　　演",string.substring("导　　演".length()+1));
                        }else if(string.indexOf("编　　剧")>-1){
                            map.put("编　　剧",string.substring("编　　剧".length()+1));
                        }else if(string.indexOf("主　　演")>-1){
                            map.put("主　　演",string.substring("主　　演".length()+1));
                        }
                    }


                    count++;
                    Map<String,Object> move = new HashMap<>();
                    System.out.println("====================================");
                    System.out.println("count = " + count);
                    move.put("desc",map);
                    move.put("img",image);
                    moves.add(move);
//                    System.out.println("url= " + "https://www.dygangs.net" + href1);
//                    System.out.println("image = " + image);
//                    String[] strings = desc.split(",");
//                    System.out.println("desc = " + Arrays.toString(strings));

                }
                Map<String,Object> m = new HashMap<>();
                m.put(type, moves);
                m.put("name", type);
                types.add(m);
            }


        }
        Move move = new Move();
        for (Map<String, Object> type : types) {
            String name = (String) type.get("name");
            System.out.println(name);
            move.setType2(name);
            List<Map<String,Object>> list = (List<Map<String, Object>>) type.get(name);
            for (Map<String, Object> map : list) {
                Map<String,String> desc = (Map<String, String>) map.get("desc");

                if(desc.get("上映日期")==null||desc.get("上映日期").length()>200){
                    continue;
                }
                if(desc.get("片　　名")==null || desc.get("片　　名").length()>200){
                    continue;
                }
                if(desc.get("编　　剧")==null||desc.get("编　　剧").length()>200){
                    continue;
                }
                if(desc.get("语　　言")==null || desc.get("语　　言").length()>200){
                    continue;
                }
                if(desc.get("类　　别")==null||desc.get("类　　别").length()>200){
                    continue;
                }
                if(desc.get("IMDb评分")==null || desc.get("IMDb评分").length()>200){
                    continue;
                }
                if(desc.get("豆瓣评分")==null||desc.get("豆瓣评分").length()>200){
                    continue;
                }
                if(desc.get("片　　长")==null || desc.get("片　　长").length()>200){
                    continue;
                }
                if(desc.get("产　　地")==null || desc.get("产　　地").length()>200){
                    continue;
                }
                if(desc.get("导　　演")==null || desc.get("导　　演").length()>200){
                    continue;
                }

                move.setImage((String) map.get("img"));
                move.setName(desc.get("片　　名"));
                move.setType(desc.get("类　　别"));
                move.setArea(desc.get("产　　地"));
                move.setDirector(desc.get("导　　演"));
                move.setLeading_actor(desc.get("主　　演"));
                move.setMovie_language(desc.get("语　　言"));
                move.setRelease_date(desc.get("上映日期"));
                move.setImdb(desc.get("IMDb评分"));
                move.setDouban(desc.get("豆瓣评分"));
                move.setLength(desc.get("片　　长"));
                move.setScreenwriter(desc.get("编　　剧"));
                System.out.println(move);
                mapper.insert(move);
            }
        }
        sqlSession.commit();
        sqlSession.close();

    }

    @Test
    public void  test1(){
//        MoveServiceImpl moveService = new MoveServiceImpl();
//        moveService.list();

    }
}
